package com.hapzxb.thread.shutdow;

import java.util.concurrent.TimeUnit;

/**
 * 结果输出
 * Count i = 619815456
 * Count i = 716265819
 *
 * 示例在执行过程中，main线程通过中断操作和cancel()方法均可使CountThread得以终止。
 * 这种通过标识位或者中断操作的方式能够使线程在终止时有机会去清理资源，而不是武断地 将线程停止，因此这种终止线程的做法显得更加安全和优雅。
 *
 * @author hhh
 * @date 2021-04-29 22:31
 */
public class Shutdown {

    public static void main(String[] args) throws InterruptedException {
        Runner one = new Runner();
        Thread countThread = new Thread(one, "CountThread");
        countThread.start();
        // 睡眠1秒，main 线程对countThread进行中断，使CountThread能感知中断而结束
        TimeUnit.SECONDS.sleep(1);
        countThread.interrupt();
        Runner two = new Runner();
        countThread = new Thread(two, "CountThread");
        countThread.start();
        // 睡眠1秒 ，main线程对Runner two 进行取消，使CountThread能够感知on为false而结束
        TimeUnit.SECONDS.sleep(1);
        two.cancel();
    }

    private static class Runner implements Runnable{
        private long i;
        private volatile  boolean  on = true;
        @Override
        public void run() {
            while (on && !Thread.currentThread().isInterrupted()) {
                i ++;
            }
            System.out.println("Count i = " + i);
        }

        private void cancel(){
            on = false;
        }



    }
}
